<?php
namespace Index\Controller;
use Think\Controller;
use Org\Util\Wechat;
class EnterController extends Controller {
    public $wx = null; 
    
    public function _initialize(){  
        $this->wx = new Wechat(C('options'));
    }
    public function index(){
        $openid = session('openid');
        if(empty($openid)){
          $this->redirect('wlg');
        //    $this->redirect('Enter/index');
        }

        //判断是否是客服,如果是就直接跳转过去
        $kefu=M('rbac_manager')->where('openid="'.$openid.'"')->count();
        if ($kefu>0) {
            $this->redirect('Kefu/inlist');
        }
        $userinfo = $this->wx->getUserInfo($openid);
        $img=$userinfo['headimgurl'];
        $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $sign = $this->wx->getJsSign($url);
        $this->assign('sign',$sing); 
        $this->assign('img',$img);
        $this->display('index');
    }


    function wlg(){ 
        $callback = 'http://sunwayuchoice.com/index.php/Enter/lgback';
        $url = $this->wx->getOauthRedirect($callback);
        header('location:'.$url);
    }
    
    function lgback(){ 
        $tk = $this->wx->getOauthAccessToken();
        session('openid',$tk['openid']);
        $this->redirect('index');
    }

    //验证是否注册过
    public function check(){
        $phone=trim(I('phone'));
        $password=md5(trim(I('password')));
        $openid=I('openid');
        $customer=M('customer');
        $res=$customer->where('phone="'.$phone.'"')->find();
        if (empty($res)) {
            $this->ajaxReturn(array('c'=>"error",'m'=>"您还没有注册,请先进行注册"));
        }
        $res=$customer->where('phone="'.$phone.'"' /*&& 'openid="'.$openid.'"'*/)->find();
        $pwd=$res['pwd'];
        if ($password!=$pwd) {
            $this->ajaxReturn(array('c'=>"false",'m'=>"密码错误"));
        }
        $this->ajaxReturn(array('c'=>"yes",'m'=>"验证成功"));
    }
    //验证登录
    public function enter(){
        $phone=trim(I('phone'));
        $password=md5(trim(I('password')));
        $openid=session('openid');
        $customer=M('customer');
		$where=array('phone'=>$phone,'openid'=>$openid);
        $res=$customer->where($where)->find();
	
        $pwd=$res['pwd'];
	
        if ($password==$pwd) {//exit('111');
        	$res=M('customer')->where('openid="'.$openid.'"')->data(array('login'=>1,))->save();
            $this->redirect('Gj/index');
        }else{//exit('222');
            echo 'password error';
        }
    }
    //一键登录
    public function login(){
        $openid=session('openid');
        //判断是否是客服,如果是就直接跳转过去
        $kefu=M('rbac_manager')->where('openid="'.$openid.'"')->count();
        if ($kefu>0) {
            $this->redirect('Kefu/inlist');
        }


        $list=M('customer')->where('openid="'.$openid.'"')->find();
        if (!empty($list)) {
        	$res=M('customer')->where('openid="'.$openid.'"')->data(array('login'=>1,))->save();
            $this->redirect('Gj/index', array('openid' => $openid));
        }else{
            $this->redirect('Register/index', array('openid' => $openid));
        }

    }
}
?>